home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-02-04 | 4.8 KB | 203 lines | [TEXT/nX^n] |
- Listing 1, "Hello World" using XLIB:
-
- #include <stdio.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- Display * mydisplay;
- Window mywindow;
- GC mygc;
- XEvent myevent;
- KeySym mykey;
- XSizeHints myhint;
- int myscreen;
- unsigned long myforeground,
- mybackground;
- XFontStruct * font_struct;
-
- /* declarations */
- char window_title[] = {"Hello World"};
-
- main (argc, argv)
- int argc;
- char **argv;
- {
- int i;
- char text[10];
- int done;
- XColor greencolor;
- XColor rgb;
-
- mydisplay = XOpenDisplay ("");
- myscreen = DefaultScreen (mydisplay);
-
- /* default pixel values */
- mybackground = WhitePixel (mydisplay, myscreen);
- myforeground = BlackPixel (mydisplay, myscreen);
-
- /* default program-specified window position and size */
- myhint.x = 10;
- myhint.y = 10;
- myhint.width = 250;
- myhint.height = 150;
- myhint.flags = PPosition | PSize;
-
- /* window creation */
- mywindow = XCreateSimpleWindow (mydisplay,
- DefaultRootWindow (mydisplay),
- myhint.x, myhint.y, myhint.width,
- myhint.height,
- 5, myforeground, mybackground);
- XSetStandardProperties (mydisplay, mywindow, window_title, window_title, None, argv, argc, &myhint);
-
- /* GC creation and initialization */
- mygc = XCreateGC (mydisplay, mywindow, 0, 0);
- XSetBackground (mydisplay, mygc, mybackground);
- XSetForeground (mydisplay, mygc, myforeground);
- XAllocNamedColor(mydisplay, DefaultColormap(mydisplay, myscreen),
- "MediumForestGreen", &greencolor, &rgb);
-
- /* font initialization */
- font_struct = XLoadQueryFont (mydisplay, "*courier-bold-r-normal--14*");
- XSetFont (mydisplay, mygc, font_struct -> fid);
-
- /* input event selection */
- XSelectInput (mydisplay, mywindow,
- ButtonPressMask | KeyPressMask | ExposureMask);
-
- /* window mapping */
- XMapRaised (mydisplay, mywindow);
- XFlush (mydisplay);
-
- /* main event-reading loop */
- done = 0;
- while (done == 0) {
-
- /* read the next event */
- XNextEvent (mydisplay, &myevent);
- switch (myevent.type) {
-
- /* repaint window on expose events */
- case Expose:
- XSetForeground(mydisplay, mygc, myforeground);
- XDrawString (mydisplay, mywindow, mygc, 10, 60, "Hello, World", 12);
- XSetForeground(mydisplay, mygc, greencolor);
- XDrawRectangle(mydisplay, mywindow, mygc, 5, 47, 150, 15);
- if (myevent.xexpose.count == 0)
- XFlush (mydisplay);
- break;
-
- /* process mouse-button presses */
- case ButtonPress:
- done = 1;
- break;
-
- /* process keyboard input */
- case KeyPress:
- i = XLookupString (&myevent, text, 10, &mykey, 0);
- if (i == 1 && text[0] == 'q')
- done = 1;
- break;
-
- default:
- break;
-
- } /* switch (myevent.type) */
- } /* while (done == 0) */
-
- /* termination */
- XFreeGC (mydisplay, mygc);
- XDestroyWindow (mydisplay, mywindow);
- XCloseDisplay (mydisplay);
- exit (0);
- }
-
- Listing 2, C program for "Hello World" using Motif:
-
- #include <X11/Xlib.h>
- #include <Mrm/MrmAppl.h>
-
- #define WINDOWNAME "Hello World"
- #define MYCLASS "HWclass"
-
- static MrmHierarchy hierarchy_id;
- static char *uid_file [] = {"uil.uid"};
- static MrmType widget_class;
- static MRMRegisterArg names_list[] =
- {
- {"hwpushbuttoncb", (caddr_t)hwpushbuttoncb },
- {0, 0}
- };
-
- Widget shellwidget;
- Widget bulletinBoard0;
-
- /* Callback routine for the pushbutton */
- void hwpushbuttoncb(w, client, call)
- Widget w;
- int client;
- XmAnyCallbackStruct *call;
- {
- exit(0);
- }
-
- /* MAIN PROGRAM */
- main(argc, argv)
- int argc;
- char **argv;
- {
- Display *display;
-
- shellwidget = XtInitialize(argv[0], "HelloWorld",NULL,0,&argc,argv);
- MrmInitialize();
- MrmOpenHierarchy((MrmCount)(1),uid_file,0, &hierarchy_id);
- MrmRegisterNames(names_list,
- (sizeof(names_list)/sizeof(MRMRegisterArg))-1);
- MrmFetchWidget(hierarchy_id, "bulletinBoard0",shellwidget,
- &bulletinBoard0, &widget_class);
- MrmCloseHierarchy(hierarchy_id);
- XtManageChild(bulletinBoard0);
- XtRealizeWidget(shellwidget);
- XtMainLoop();
- }
-
- Listing 3, UIL code for "Hello World" using Motif:
-
- module helloworld
- version = 'V1.0'
- names = case_sensitive
-
- include file 'XmAppl.uil';
-
- procedure hwpushbuttoncb();
-
- object bulletinBoard0 : XmBulletinBoard widget {
- arguments {
- XmNborderWidth = 0;
- XmNmarginWidth = 1;
- XmNmarginHeight = 1;
- XmNresizePolicy = XmRESIZE_NONE;
- XmNx = 0;
- XmNy = 0;
- XmNwidth = 152;
- XmNheight = 96;
- };
- controls {
- XmPushButton pushButton5 ;
- };
- };
- object pushButton5 : XmPushButton widget {
- arguments {
- XmNbackground = color('MediumForestGreen');
- XmNfontList = font('*helvetica-bold-r-normal--14*');
- XmNlabelString = 'Hello, World';
- XmNx = 18;
- XmNy = 18;
- };
- callbacks {
- XmNactivateCallback = procedure hwpushbuttoncb();
- };
- };
- end module;
-
-